package com.aistar.service.impl;

import com.aistar.mapper.ContestMessageMapper;
import com.aistar.pojo.ContestMessage;
import com.aistar.pojo.ContestMessageExample;
import com.aistar.service.ContestMessageService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.List;

@Service
public class ContestMessageServiceImpl implements ContestMessageService {


    private ContestMessageMapper contestMessageMapper;

    @Autowired
    public ContestMessageServiceImpl(ContestMessageMapper contestMessageMapper){
        this.contestMessageMapper=contestMessageMapper;
    }


    @Override
    public ServerResponse getById(Integer id) {
        ContestMessage contestMessage = contestMessageMapper.selectByPrimaryKey(id);
        if (contestMessage!=null)
            return ServerResponse.getSuccess(contestMessage);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse modify(ContestMessage contestMessage) {
        int rows = contestMessageMapper.updateByPrimaryKeySelective(contestMessage);
        if (rows>0)
            return ServerResponse.modifySuccess();
        return ServerResponse.modifyFailed();
    }

    @Override
    public ServerResponse save(ContestMessage contestMessage) {
        int row = contestMessageMapper.insertSelective(contestMessage);
        if (row >0)
            return ServerResponse.addSuccess();
        return ServerResponse.addFailed();
    }

    //根据竞赛id和用户id查找用户信息表（正赛）
    @Override
    public ServerResponse getByContestIdAndCustomerId(Integer contestId, Integer customerId) {
        ContestMessageExample example = new ContestMessageExample();
        example.createCriteria().andContestIdEqualTo(contestId).andCustomerIdEqualTo(customerId).andStatusEqualTo(1);
        example.setOrderByClause("contest_msg_start_time DESC");
        List<ContestMessage> contestMessages =contestMessageMapper.selectByExample(example);
        if ( contestMessages!= null && contestMessages.size()>0 )
            return ServerResponse.getSuccess(contestMessages.get(0));
        return ServerResponse.getFailed();
    }
    //查找关于虚拟竞赛的竞赛信息
    @Override
    public ServerResponse getByContestIdAndCustomerIdAndStatus(Integer contestId,Integer customerId) {
        ContestMessageExample example = new ContestMessageExample();
        example.createCriteria().andContestIdEqualTo(contestId).andCustomerIdEqualTo(customerId).andStatusEqualTo(2);
        example.setOrderByClause("contest_msg_start_time DESC");
        List<ContestMessage> contestMessages =contestMessageMapper.selectByExample(example);
        if ( contestMessages!= null && contestMessages.size()>0 )
            return ServerResponse.getSuccess(contestMessages.get(0));
        return ServerResponse.getFailed();
    }
}
